"""
@Project:p8_syl_flask
@File:dev.py
@Author:郑凯乐
@Date:16:48
"""
import pickle
import redis
from config.default import DefaultConfig

class DevConfig(DefaultConfig):
    DEBUG = True
    SQLALCHEMY_DATABASE_URI= "mysql://root:123456@127.0.0.1:3306/p8_flask"
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    SQLALCHEMY_ECHO = False

class Redis:
    @staticmethod
    def connect():
        r = redis.Redis(host='127.0.0.1',password=123, port=6379,db=0)
        return r

    # 将内存数据二进制通过序列号转为文本流，再存入redis
    @staticmethod
    def set_data(r, key, data, ex=None):
        r.set(key, pickle.dumps(data), ex)

    # 将文本流从redis中读取并反序列化，返回返回
    @staticmethod
    def get_data(r, key):
        data = r.get(key)
        if data is None:
            return None
        return pickle.loads(data)
